package app.dao;

import app.util.Util;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service("deviceDao")
public class DeviceDao extends BaseDao {
	// 查询
	public JSONObject list(String data) {
		JSONObject dataObj = JSONObject.fromObject(data);
		int currentPage = Integer
				.parseInt((dataObj.getString("currentPage").equals("") ? "0" : dataObj.getString("currentPage")));
		int pageSize = Integer.parseInt(dataObj.getString("pageSize"));
		String searchText = dataObj.getString("searchText");
		int begin = (currentPage - 1) * pageSize;
		int end = (currentPage) * pageSize;
		String sql = "SELECT * FROM device WHERE 1=1 ";
		String sqlCount = "select count(*) as count from device WHERE 1=1 ";
		if (!searchText.equals("")) {
			String searchTextWhere = " and device.name like('%" + searchText + "%')";
			sql += searchTextWhere;
			sqlCount += searchTextWhere;
		}
		sql += " order by create_time desc";
		sql += "  limit " + begin + "," + pageSize + "";

		// String sqlOracle = "SELECT * FROM ( SELECT result.*, ROWNUM RN FROM ("+sql+")
		// result WHERE ROWNUM <= "+end+") WHERE RN >= "+begin;

		JSONObject dataRtn = new JSONObject();
		dataRtn.put("count", count(sqlCount));
		dataRtn.put("data", listBase(sql));

		return dataRtn;
	}

	// 获取查询总数
	public String count(String sql) {
		return listBase(sql).getJSONObject(0).getString("count");
	}

	/**
	 * 根据ID查询
	 * 
	 * @param id
	 * @return
	 */
	public JSONObject getById(String id) {
		JSONObject result = new JSONObject();
		JSONArray ja = listBase("select * from s_user where id='" + id + "'");
		if (ja.size() > 0) {
			result = ja.getJSONObject(0);
		}
		return result;
	}

	// 登录
	public JSONObject login(String userJson) {
		JSONObject user = new JSONObject();

		JSONObject json = JSONObject.fromObject(userJson);
		String name = json.getString("name");
		String password = json.getString("password");
		String sql = "select * from s_user where  name='" + name + "' and  password='" + password + "' and status ='1'";
		JSONArray users = listBase(sql);
		if (!users.isEmpty()) {
			user = users.getJSONObject(0);
			String roleId = user.getString("roleId");
			sql = "select * from s_role where id='" + roleId + "'";
			JSONArray roles = listBase(sql);
			if (!roles.isEmpty()) {
				JSONObject role = roles.getJSONObject(0);
				user.put("role", role);
				sql = "SELECT * FROM s_menu WHERE id IN(SELECT menuId FROM s_role_menu WHERE roleId='"
						+ role.getString("id") + "') order by sort";
				JSONArray menu = listBase(sql);
				user.put("menu", menu);
			}
			// 位置信息
			// sql="select * from s_param where id=1";
			// user.put("positionInfo", listBase(sql).getJSONObject(0).getString("value"));
		}
		return user;
	}

	/**
	 * 保存，修改
	 * 
	 * @param data
	 * @param currentUserName
	 * @return 受影响行数
	 */
	public boolean save(String data, String currentUserName) {
		boolean rtn = false;

		JSONObject user = JSONObject.fromObject(data);
		String id = user.getString("id");
		String name = user.getString("name");
		String password = user.getString("password");
		String relName = user.getString("relName");
		String roleId = user.getString("roleId");
		String phone = user.getString("phone");

		String sql = "";

		if (id.equals("")) {// 添加
			id = Util.uuid();
			sql = "INSERT  INTO s_user(id,name,password,status,roleId,relName,phone,createUser,createTime) VALUES ('"
					+ id + "','" + name + "','" + password + "','1','" + roleId + "','" + relName + "','" + phone
					+ "','" + currentUserName + "'," + new Date().getTime() + ")";
			rtn = execute(sql);
		} else {// 修改
			sql = "update s_user set roleId='" + roleId + "',relName='" + relName + "',phone='" + phone + "' where id='"
					+ id + "'";
			rtn = execute(sql);
		}

		return rtn;
	}

	/**
	 * 重置密码
	 * 
	 * @param data
	 * @return
	 */
	public boolean resetPassword(String data) {
		boolean rtn = false;

		JSONObject user = JSONObject.fromObject(data);
		String id = user.getString("id");
		String password = user.getString("password");

		String sql = "";

		sql = "update s_user set password='" + password + "' where id='" + id + "'";
		rtn = execute(sql);

		return rtn;
	}

	/**
	 * 开启，禁用
	 * 
	 * @param data
	 * @return
	 */
	public boolean offOrOnUser(String data) {
		boolean rtn = false;

		JSONObject user = JSONObject.fromObject(data);
		String id = user.getString("id");
		String status = user.getString("status");

		String sql = "";

		sql = "update s_user set status='" + status + "' where id='" + id + "'";
		rtn = execute(sql);

		return rtn;
	}

	/**
	 * 删除
	 * 
	 * @param data {ids: "01,7c1c6549d0894cbfbac1a50482a0f906"}
	 * @return 受影响行数
	 */
	public boolean delByIds(String data) {
		JSONObject idsObj = JSONObject.fromObject(data);
		String[] ids = idsObj.getString("ids").split(",");
		String sql = "delete from s_user where id in (";
		for (int i = 0; i < ids.length; i++) {
			String id = ids[i];
			if (i == (ids.length - 1)) {
				sql += "\'" + id + "\')";
			} else {
				sql += "\'" + id + "\',";
			}
		}
		return execute(sql);
	}

	// 用户重复检查
	public int userNameCheck(String data) {
		JSONObject user = JSONObject.fromObject(data);
		String name = user.getString("name");
		String sql = "select name from s_user where name='" + name + "'";
		return listBase(sql).size();
	}

	/**
	 * 修改当前用户密码
	 * 
	 * @param data
	 * @return
	 */
	public boolean changeCurrentUserPassword(String data) {
		return resetPassword(data);
	}

}
